package com.usc.csci588.chaltrojan.login;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.usc.csci588.chaltrojan.R;
import com.usc.csci588.chaltrojan.photoupload.S3UploaderActivity;

public class MainMenuActivity extends Activity {	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//myTrip = new Trip();
		setContentView(R.layout.main_menu_layout);
		Toast.makeText(getBaseContext(), "Welcome to the Main Menu!",
				Toast.LENGTH_LONG).show();
		
		SharedPreferences settings = getSharedPreferences("HANDLE",0);
		String handle = settings.getString("handle", "default_user");				
		//Toast.makeText(getBaseContext(), handle + " WELCOME!!",Toast.LENGTH_LONG).show();
		
		setupListMenu();
	
	}
	
	public void setupListMenu()
	{
		ListView lv = (ListView) findViewById(R.id.main_menu_list_view);
		ArrayAdapter<String> main_menu_adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, getResources()
						.getStringArray(R.array.main_menu_items));
		lv.setAdapter(main_menu_adapter);		

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				String item = ((TextView) view).getText().toString();
				Resources res = getResources();
				String[] menu_items = res.getStringArray(R.array.main_menu_items);
				
				if (item.equals(menu_items[0])) // create trip
				{					
					Toast.makeText(getBaseContext(),"Let's create a new trip.", Toast.LENGTH_LONG).show();
					Intent i = new Intent(getApplicationContext(),CreateNewTripFormActivity.class);	            
			        startActivity(i);
				}
				if(item.equals(menu_items[1])) // view my trips
						{
					Toast.makeText(getBaseContext(),"Viewing my trips", Toast.LENGTH_LONG).show();
					Intent i = new Intent(getApplicationContext(),ViewMyTripsActivity.class);	            
			        startActivity(i);				
					
						}
				if(item.equals(menu_items[2])) // view my trips
				{				
					Toast.makeText(getBaseContext(),"Join a trip", Toast.LENGTH_LONG).show();
					Intent i = new Intent(getApplicationContext(),JoinTripActivity.class);	            
			        startActivity(i);
					
				}
//				if(item.equals(menu_items[3])) // take photo
//				{				
//					//Toast.makeText(getBaseContext(),"Join a trip", Toast.LENGTH_LONG).show();
//					Intent i = new Intent(getApplicationContext(),S3UploaderActivity.class);	            
//			        startActivity(i);
//					
//				}
				// Toast.makeText(getBaseContext(), item,
				// Toast.LENGTH_LONG).show();
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.log_out:
			logout();			
			return true;
		case R.id.exit_app:
			exitApp();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	
	public void logout()
	{
		Toast.makeText(getApplicationContext(), "Logging out....", Toast.LENGTH_SHORT);
		Intent i = new Intent(getApplicationContext(),TwitterLoginActivity.class);	            
        startActivity(i);		
		
	}
	
	public void exitApp()
	{
	
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
		
		
	}
	
}
